package com.youxin.cpcenter.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.ent.MemberIdentityBean;
import com.youxin.busfoundation.bean.ent.RequestEntIdentifyInfo;
import com.youxin.busfoundation.bean.ent.RequestMemberInfo;
import com.youxin.cpcenter.bean.MemberQueryListBean;
import com.youxin.cpcenter.bean.member.MemberInfo;
import com.youxin.cpcenter.common.log.LogMethod;
import com.youxin.cpcenter.model.TbMemberInfo;
import com.youxin.cpcenter.service.EnterpriseService;
import com.youxin.foundation.utils.ObjectUtils;
import com.youxin.foundation.utils.RegexUtils;

/**
 * 会员中心管理 企业认证接口
 * 
 * @author yingde.cao
 *
 */
@RestController
public class MemberManagerController extends BaseController{
	
	@Autowired
	private EnterpriseService enterpriseService;	
	/**
	 * 企业公司认证接口
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param bean RequestEntIdentifyInfo
	 * @return ResponseObject
	 */
	@LogMethod(id="ent.member.identify.create",eventContent={})
	@RequestMapping(value = "/api/ent/identify", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject identify(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse
			,@RequestBody RequestEntIdentifyInfo bean) {		
		try {	
			ObjectUtils.requestNonNull(bean, "参数不能为空");		
			ObjectUtils.requestNonEmpty(bean.getCompanyUid(), "公司uid不能为空");		
			ObjectUtils.requestNonEmpty(bean.getFullName(), "公司名称不能为空");		
			//ObjectUtils.reququestCondition(bean.getMemberId()>0L, "用户id不能为空");		
			//ObjectUtils.requestNonEmpty(bean.getMemberName(), "用户名不能为空");		
			
			return enterpriseService.requestIdentify(bean);
		}
		catch (DuplicateKeyException e) {
			logger.error("/identify企业认证错误:", e);
			return ResponseObject.userFailure("当前企业已经被认证");
		}
		catch (Exception e) {
			logger.error("/identify企业认证错误:", e);
			return ResponseObject.systemError(e);
		}
	}

	@LogMethod(id="ent.member.add",eventContent={})
	@RequestMapping(value = "/api/ent/addmember", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject addMember(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,@RequestBody RequestMemberInfo bean) {		
		try {
			ObjectUtils.requestNonEmpty(bean.getName(), "姓名不能为空");
			ObjectUtils.requestNonEmpty(bean.getPosition(), "职位不能为空");			
			if(!RegexUtils.isMobile(bean.getMobile()))
			{
				return ResponseObject.userFailure("手机号码格式不正确");
			}
			TbMemberInfo tbMemberInfo=enterpriseService.addMember(bean);
			if(tbMemberInfo==null)
			{
				return ResponseObject.userFailure("添加会员失败");
			}
			return ResponseObject.successObject(tbMemberInfo.getId());
		}
		catch (DuplicateKeyException e) {
			logger.error("/identify企业认证错误:", e);
			return ResponseObject.userFailure("当前成员已经是企业会员");
		}
		catch (Exception e) {
			logger.error("/identify企业认证错误:", e);
			return ResponseObject.systemError(e);
		}
	}
	/**
	 * 企业列表
	 * @description 
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param bean
	 * @return
	 */
	@LogMethod(id="ent.member.list",eventContent={})                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
	@RequestMapping(value = "/api/member/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject listMembers(HttpServletRequest httpServletRequest, 
			HttpServletResponse httpServletResponse,@RequestBody MemberQueryListBean bean) {		
		try {			
			List<MemberInfo> tbMemberInfo=enterpriseService.listMembers(bean);
			if(tbMemberInfo==null)
			{
				return ResponseObject.userFailure("显示成员列表失败");
			}
			return ResponseObject.successObject(bean);
		} catch (Exception e) {
			logger.error("/api/member/list企业认证错误:", e);
			return ResponseObject.systemError(e);
		}
	}
	/**
	 * 开通企业版
	 * @description 
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param bean
	 * @return
	 */
	@LogMethod(id="ent.member.edition.open.",eventContent={})
	@RequestMapping(value = "/api/member/edition/open", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject openEdition(HttpServletRequest httpServletRequest, 
			HttpServletResponse httpServletResponse,@RequestBody MemberIdentityBean bean) {		
		try {
			return enterpriseService.openEdition(bean);
		}catch (DuplicateKeyException e) {
			logger.error("/identify企业认证错误:", e);
			return ResponseObject.userFailure("当前成员已经是企业会员");
		}
		catch (Exception e) {
			logger.error("/api/member/edition/open开通企业版错误:", e);
			return ResponseObject.systemError(e);
		}
	}
	/**
	 * 关闭企业版
	 * @description 
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param bean
	 * @return
	 */
	@LogMethod(id="ent.member.edition.close.",eventContent={})
	@RequestMapping(value = "/api/member/edition/close", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject closeEdition(HttpServletRequest httpServletRequest, 
			HttpServletResponse httpServletResponse,@RequestBody MemberIdentityBean bean) {		
		try {
			return enterpriseService.closeEdition(bean);
		} catch (Exception e) {
			logger.error("/api/member/edition/close关闭企业版错误:", e);
			return ResponseObject.systemError(e);
		}
	}
	/**
	 * 认证企业版会员
	 * @description 
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param bean
	 * @return
	 */
	@LogMethod(id="ent.member.edition.auth.",eventContent={})
	@RequestMapping(value = "/api/member/edition/auth", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject authEdition(HttpServletRequest httpServletRequest, 
			HttpServletResponse httpServletResponse,@RequestBody MemberIdentityBean bean) {		
		try {
			return enterpriseService.authenticate(bean);
		} catch (Exception e) {
			logger.error("/api/member/edition/auth会员认证错误:", e);
			return ResponseObject.systemError(e);
		}
	}
	/**
	 * 解除认证
	 * @description 
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param bean
	 * @return
	 */
	@LogMethod(id="ent.member.edition.unauth.",eventContent={})
	@RequestMapping(value = "/api/member/edition/unauth", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject unauthEdition(HttpServletRequest httpServletRequest, 
			HttpServletResponse httpServletResponse,@RequestBody MemberIdentityBean bean) {		
		try {
			return enterpriseService.unAuthenticate(bean);
		} catch (Exception e) {
			logger.error("/api/member/edition/unauth错误:", e);
			return ResponseObject.systemError(e);
		}
	}
	/**
	 * 企业成员转移
	 * @description 
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param bean
	 * @return
	 */
	@LogMethod(id="ent.member.transfer.admin",eventContent={})
	@RequestMapping(value = "/api/ent/manager/transfer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject transferManager(HttpServletRequest httpServletRequest, 
			HttpServletResponse httpServletResponse,@RequestBody MemberIdentityBean bean) {		
		try {
			return enterpriseService.transferManager(bean);
		} catch (Exception e) {
			logger.error("/api/member/edition/unauth错误:", e);
			return ResponseObject.systemError(e);
		}
	}
	
}
